package com.lc.controller;

import com.lc.mapper.UserMapper;
import com.lc.pojo.User;
import com.lc.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;

@Controller
public class RegisterController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    @RequestMapping("/register")
    public String register(Model model){
        return "register";
    }

    @RequestMapping("/add")
    public String addUser(HttpServletRequest request, User user) throws Exception {
        User userNew = new User();
        BeanUtils.copyProperties(user, userNew);
        Boolean succ = userService.addUser(userNew);
        if (succ) {
            String successMsg = URLEncoder.encode("增加成功", "UTF-8");
            return "redirect:/user?successMsg=" + successMsg;
        } else {
            String errorMsg = URLEncoder.encode("增加失败", "UTF-8");
            return "redirect:/user?successMsg=" + errorMsg;
        }
    }

}
